﻿using System.Diagnostics;
using System.Linq;
using System.Web.Mvc;
using Sharp.Platform.Mvc.Models;
using Sharp.Platform.Owin;

namespace Sharp.Platform.Mvc.Filter
{
    [DebuggerStepThrough]
    public class ViewEnginesResultFilter : IResultFilter, IExceptionFilter
    { 
        public void OnResultExecuted(ResultExecutedContext filterContext)
        {
        }

        public void OnResultExecuting(ResultExecutingContext filterContext)
        {
            var request = filterContext.RequestContext;
            var viewData = filterContext.Controller.ViewData;
            var res = filterContext.Result as ViewResultBase;

            var baseViewModel = viewData.Model as BaseViewModel;
            if (baseViewModel != null)
            {
                if (request.HttpContext.User.Identity.IsAuthenticated)
                    baseViewModel.OP = SharpUserManager.Current.Id;

                if (request.RouteData.Values.Values.Any(x => x.ToString() == "Create"))
                {
                    baseViewModel.PageTitle = "新增";
                }
                else if (request.RouteData.Values.Values.Any(x => x.ToString() == "Details"))
                {
                    baseViewModel.PageTitle = "详情";
                }
                else if (request.RouteData.Values.Values.Any(x => x.ToString() == "Edit"))
                {
                    baseViewModel.PageTitle = "编辑";
                }
            }
            var baseListViewModel = viewData.Model as BaseListViewModel;

            if (baseListViewModel != null)
            {

            }
        }
         
        public void OnException(ExceptionContext filterContext)
        {
            //SetupSkinViewEngines(filterContext.RequestContext, filterContext.Controller.ViewData, filterContext.Result as ViewResultBase);
        }
         
    }
}
